06. Solution: Defining Custom Annotations

Solution: Defining Custom Annotations

ND079 JPND C2 L04 A06 Solution Defining Custom Annotations

Test.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {
}

CalculatorTest.java

public final class CalculatorTest implements UnitTest {

  private final Calculator calculator = new Calculator();

  @Override
  public void beforeEachTest() {
    calculator.registerOperation("+", (a, b) -> a + b);
    calculator.registerOperation("-", (a, b) -> a - b);
    calculator.registerOperation("/", (a, b) -> a / b);
    calculator.registerOperation("*", (a, b) -> a * b);
  }

  @Test
  public void testAddition() {
    assert calculator.calculate(1, "+", 1) == 2;
    System.out.println("test passed");
  }

  @Test
  public void testSubtraction() {
    assert calculator.calculate(45, "-", 43) == 2;
  }
}